package com.ulook.lookplanet.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.qmuiteam.qmui.widget.QMUIRadiusImageView2;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.ulook.lookplanet.R;
import com.ulook.lookplanet.base.WidgetData;
import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.ui.matching.VMatchingCard;
import com.ulook.lookplanet.ui.notifications.UserDetail.UserDetailActivity;
import com.ulook.lookplanet.util.StringUtil;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.ResponseCallback;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/5/11
 */
public class MatchingCardView extends LinearLayout implements WidgetData<VMatchingCard> {

    private Context context;
    private int currUserId;
    private VMatchingCard matchingCard;

    public MatchingCardView(Context context) {
        super(context);
        initMatchingCard(context);
    }

    public MatchingCardView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initMatchingCard(context);
    }

    public MatchingCardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initMatchingCard(context);
    }

    private void initMatchingCard(Context context){
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.widget_matching_card, this, true);
    }

    @Override
    public void setWidgetData(VMatchingCard value) {
        this.matchingCard = value;
        QMUIRadiusImageView2 head = findViewById(R.id.w_card_head_img);
        TextView name = findViewById(R.id.w_card_name_tx);
        TextView planet = findViewById(R.id.w_card_planet);
        TextView gender = findViewById(R.id.w_card_gender);
        TextView star = findViewById(R.id.w_card_star_name);
        QMUIRoundButton hpd = findViewById(R.id.w_card_hpd);

        // 名字
        name.setText(matchingCard.getName());
        // 用户头像
        if(!StringUtil.isEmpty(matchingCard.getHeadimg())){
            ResponseCallback callback = new ResponseCallback() {
                @Override
                public void onSuccess(Object res) {
                    byte[] image = (byte[]) res;
                    Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
                    head.setImageBitmap(bitmap);
                }
                @Override
                public void onFailure(OkHttpException failuer) {
                    ThrowException.print(failuer, failuer.getEmsg());
                }
            };
            AsynRequestUtil.loadImg(AppServer.HEAD_IMG_PATH + matchingCard.getHeadimg(), null, callback);
        }
        // 所属星球
        planet.setText(matchingCard.getPlanet());
        // 性别
        if(!StringUtil.isEmpty(matchingCard.getGender())){
            if("男".equals(matchingCard.getGender())){
                gender.setText(R.string.matching_man);
            }else {
                gender.setText(R.string.matching_woman);
            }

        }else {
            gender.setVisibility(GONE);
        }
        // 星座
        if(!StringUtil.isEmpty(matchingCard.getStarsign())){
            star.setText(matchingCard.getStarsign());
        }else {
            star.setVisibility(GONE);
        }
        // 合拍度
        hpd.setText(matchingCard.getHpd() + "%");

        // 点击跳转用户详情
        this.setOnClickListener(view -> {
            Intent intent = new Intent(context, UserDetailActivity.class);
            intent.putExtra("user", matchingCard.getId());
            intent.putExtra("currUser", currUserId);
            context.startActivity(intent);
        });
    }

    @Override
    public void setWidgetData(VMatchingCard value, int currUserID) {
        this.currUserId = currUserID;
        setWidgetData(value);
    }
}
